<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>知识库导入</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="format-detection" content="telephone=no">
		<!-- jQuery-->
		<script type="text/javascript" src="/static/plugins/jquery/jquery-3.6.0.min.js"></script>

		<!-- layui-->
		<link rel="stylesheet" href="/static/plugins/layui/2.8.4/css/layui.css" media="all">
		<script src="/static/plugins/layui/2.8.4/layui.js" charset="utf-8"></script>
		
		
	</head>
	<style>
		.all-view{
			width: 90%;
			margin: 0 auto;
			margin-top: 20px;
		}
		.btn-view{
			margin-bottom: 10px;
			
		}
		.down-example{
			margin-top: 7px; 
			color: #00aaff;
		}
		.upload-tip{
			float: right;
		}
		.add-btn{
			width:120px;
		}
		.bottom-view{
			position: fixed;
			bottom: 0;
			width: 100%;
		}
		.add-btn-view{
			width: 260px;
			margin: 0 auto;
			margin-bottom: 40px;
		}
		.layui-form-switch {
			margin-top: -3px !important;
		}
	</style>
	
	<body>
		
		<!--头部输入框-->
		
		<form class="layui-form layui-form-pane all-view" action="">

			<div class="layui-form-item">
				<label class="layui-form-label">选择分类</label>
				<div class="layui-input-block">
					<select name="chooseType" id="chooseType" lay-filter="chooseType">
						<option value="">请选择</option>

					</select>
				</div>
			</div>

			<div class="layui-form-item">
				<span style="line-height: 30px;">是否去除连续的空格与换行<i class="layui-icon layui-icon-question" id="isSimpleCheckTip" style="color: #ffa33a;padding-left: 5px; cursor: pointer;" ></i>：</span>
				<input type="checkbox" name="isSimpleCheck" lay-skin="switch" lay-filter="switchIsSimple" title="是|否" checked>
			</div>

			<div class="layui-form-item">
				<span style="line-height: 30px;">分段说明<i class="layui-icon layui-icon-question" id="burstTip" style="color: #ffa33a;padding-left: 5px; cursor: pointer;" ></i>：</span>
			</div>

			<div class="layui-upload-drag" style="display: block;" id="ID-upload-demo-drag">
			  <i class="layui-icon layui-icon-upload"></i> 
			  <div>点击上传，或将文件拖拽到此处</div>
			  <div class="layui-hide" id="ID-upload-demo-preview">
			    <hr> <span id="uploadFileName"></span>
			  </div>
			</div>
			
			  <div class="upload-tip">支持：txt、pdf、doc、docx</div>
			  
			  
			 
		</form>
		
	<div class="bottom-view">
		<div class="layui-btn-container add-btn-view">
			<button type="button" class="layui-btn layui-btn-primary layui-border-blue add-btn" onclick="toAdd();">确定</button>
			<button type="button" class="layui-btn layui-btn-primary layui-border add-btn" onclick="toClose();">取消</button>
		</div>
	</div>

	<input type="hidden" name="baseId" id="baseId" value=""><!--知识库id-->
	<input type="hidden" name="typeId" id="typeId" value=""><!--所选择的类型id-->
	<input type="hidden" name="isRemoveBlank" id="isRemoveBlank" value="1"><!--是否去除连续的空格与换行，0否1是-->
	</body>


	<script>
		$('#baseId').val('[(${dto.id})]');
		var information = '';

		$('#id').val('[(${dto.id})]');

		var tipIndex = null;
		$("#isSimpleCheckTip").mouseover(function() {
			tipIndex = layer.tips("系统在分段前会自动帮你去除连续的空格、换行，建议开启。", this, {tips: [2, "#C1C1C1"],time:0});
		});
		$("#isSimpleCheckTip").mouseout(function() {layer.close(tipIndex);});
		$("#burstTip").mouseover(function() {
			tipIndex = layer.tips("推荐以中文感叹号（！，注意区分英文感叹号!）对输入的内容进行分段，每个段落300字以内。若无分段，则系统会自动以300个字数进行分段。", this, {tips: [2, "#C1C1C1"],time:0});
		});
		$("#burstTip").mouseout(function() {layer.close(tipIndex);});


		layui.use(function(){
			var upload = layui.upload;
			var form = layui.form;
			var $ = layui.$;
			// 渲染
			upload.render({
				elem: '#ID-upload-demo-drag',
				url: '/api/inter/knowledgeList/uploadLedgeFileList', // 实际使用时改成您自己的上传接口即可。
				accept: 'file',
				exts:'txt|pdf|doc|docx',
				done: function(res){
					console.log('上传返回',res);
					if(res.code=='SUCCESS'){
						layer.msg('上传成功');
						$('#ID-upload-demo-preview').removeClass('layui-hide');
						var fileName = res.fileName;
						$('#uploadFileName').text('上传成功：'+fileName);
						information =  res.fileInfo;
					}else if(res.code=='PARAMSERROR'){
						layer.msg('不支持的文件格式，只支持(DOC/DOCX/PDF/TXT)', {icon: 2});
						information = '';
					}else {
						layer.msg('上传失败，请稍后重试！', {icon: 2});
						information = '';
					}


				}
			});
			form.on('switch(switchIsSimple)', function(data){
				var elem = data.elem; // 获得 checkbox 原始 DOM 对象
				if(elem.checked){//是
					$('#isRemoveBlank').val(1);
				}else {//否
					$('#isRemoveBlank').val(0);
				}

			});
			form.on('select(chooseType)', function(data){
				//var elem = data.elem; // 获得 select 原始 DOM 对象
				var value = data.value; // 获得被选中的值
				//var othis = data.othis; // 获得 select 元素被替换后的 jQuery 对象
				$('#typeId').val(value);
			});
		});


		$.ajax({
			url : '/api/inter/knowledgeType/searchType',
			data : {id:$('#baseId').val()},
			type : 'POST',
			async : false,
			success : function(result) {
				console.log('result',result);

				if(result.code=='0'){
					var eKnowKnowledgeTypeList = result.data;
					if(eKnowKnowledgeTypeList!=null && eKnowKnowledgeTypeList.length>0){
						for(var i=0;i<eKnowKnowledgeTypeList.length;i++){
							$('#chooseType').append('<option value="' + eKnowKnowledgeTypeList[i].id + '">' + eKnowKnowledgeTypeList[i].typeName + '</option>');
						}

					}
				}else{
					layer.msg('不好意思，系统出现问题了，请稍后重试！', {icon: 2});
					setTimeout(function(){
						window.parent.layer.closeAll();
					}, 1000);
				}
			}
		});


		function toClose() {
			window.parent.layer.closeAll();
		}

		function toAdd() {
			var typeId = $('#typeId').val();
			if(typeId ==undefined || typeId ==null || typeId =='' ){
				layer.msg('请选择分类', {icon: 0});
				return ;
			}
			if(information ==undefined || information ==null || information =='' ){
				layer.msg('请上传文件', {icon: 0});
				return ;
			}

			var layindex= layer.load(2);
			$.ajax({
				url: '/api/inter/knowledgeList/addOrUpdateKnowledgeList',
				data: {
					'addOrUpdate':'add',
					'baseId':$('#baseId').val(),
					'typeId':typeId,
					'information':information,//JSON.stringify(information),
					'isRemoveBlank':$('#isRemoveBlank').val()
				},
				type: 'POST',
				async: true,
				timeout: 60000,
				success: function (result) {
					layer.close(layindex);
					console.log('result:',result);
					if(result.code=='SUCCESS'){
						layer.msg("操作成功", {icon: 1});
						setTimeout(function(){
							window.parent.location.reload();
						}, 1000);
					}else if(result.code=='NOBASE'){
						layer.close(layindex);
						layer.msg('不好意思，未定位到知识库，请稍后重试！', {icon: 2});
					} else{//失败
						layer.close(layindex);
						layer.msg('不好意思，系统出现问题了，请稍后重试！', {icon: 2});
					}
				}, error: function () {
					layer.close(layindex);
					layer.msg('不好意思，系统出现问题了，请稍后重试！', {icon: 2});
				}
			});
		}

	</script>
</html>